package com.achievement.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.achievement.dao.CourseRepository;
import com.achievement.dao.SubjectRepository;
import com.achievement.entity.Subject;
import com.common.util.TablePage;
import com.user.dao.TeacherRepository;
@Service
@Transactional
public class SubjectService implements ISubjectService {
	@Autowired
	private SubjectRepository subjectDao;
	@Autowired
	private CourseRepository courseDao;
	@Autowired
	private TeacherRepository teacherDao;
	
	public List<Subject> findAll() {
		return (List<Subject>) subjectDao.findAll();
	}

	@Override
	public TablePage<Subject> findPage(Specification<Subject> spec, Pageable pageable) {
		Page<Subject> page = subjectDao.findAll(spec, pageable);
		return TablePage.getTablePage(page);
	}

	@Override
	public void saveOrUpdate(Subject subject) {
		// TODO Auto-generated method stub
		subjectDao.save(subject);
	}

	@Override
	public boolean hasCourse(Long id) {
		if(courseDao.findBySubjectId(id).size()>0)return true;
		return false;
	}

	@Override
	public boolean hasTeacher(Long id) {
		// TODO Auto-generated method stub
		if(teacherDao.findBySubjectId(id).size()>0)return true;
		return false;
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		subjectDao.delete(id);
	}

	@Override
	public boolean isNumExist(Subject subject) {
		Subject _s = subjectDao.findByNum(subject.getNum());
		if(null!=_s&&_s.getId()!=subject.getId())return true;
		return false;
	}

}
